package com.getjsp.adapter;

import java.util.ArrayList;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;

public class MyViewPagerAdapter extends FragmentStatePagerAdapter {
	private ArrayList<Fragment> list;
	FragmentManager fm;

	public MyViewPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments) {
		super(fm);
		this.list = fragments;
	}

	@Override
	public Fragment getItem(int pos) {

		return list.get(pos);
	}

	@Override
	public int getCount() {

		return list.size();
	}

	@Override
	public int getItemPosition(Object object) {
		// TODO Auto-generated method stub
		return super.getItemPosition(object);
	}

	public void setFragments(ArrayList<Fragment> fragments) {
		if (this.list != null) {
			FragmentTransaction ft = fm.beginTransaction();
			for (Fragment f : this.list) {
				ft.remove(f);
			}
			ft.commit();
			ft = null;
			fm.executePendingTransactions();
		}
		this.list = fragments;
		notifyDataSetChanged();
	}
}
